﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using teavch.Util.SitemapWriter;

namespace teavch.Util.SitemapWriterSampleWebMVC.Controllers
{
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			ViewBag.Message = "Welcome to Sitemap Generator!";

			return View();
		}

		public ActionResult About()
		{
			return View();
		}

		[HttpGet]
		public ActionResult GenerateSitemap()
		{
			ViewBag.Message = "Welcome to Sitemap Generator!";
			return View();
		}

		//[HttpPost]
		//public ContentResult GenerateSitemap(Models.SitemapModel model)
		//{
		//	Crawler crawler = new Crawler();
		//	try
		//	{
		//		Session[ "sitemapUrl" ] = model.Url;
		//		crawler.IncludeOptions.IncludeImage = true;
		//		//crawler.IncludeLastModifiedTag = true;
		//		//crawler.IncludeChangeFrequencyTag = true;
		//		//crawler.IncludePriorityTag = true;
		//		crawler.ExcludeUrlsContaining = new List<string>();
		//		crawler.ExcludeUrlsContaining.Add( "/account" );
		//		crawler.ExcludeUrlsContaining.Add( "/authentication" );

		//		crawler.ExcludeImageUrlsContaining = new List<string>();
		//		crawler.ExcludeImageUrlsContaining.Add( "/images/" );
		//		crawler.ExcludeImageUrlsContaining.Add( "/account-images/" );

		//		crawler.CrawlingCompleted += new CrawlingCompletedEventHandler( CrawlingCompleted_CrawlingCompletedEventHandler );
		//		crawler.CrawlingStatusChanged += new CrawlingStatusChangedEventHandler( CrawlingStatusChanged_CrawlingStatusChangedEventHandler );

		//		crawler.CrawlAsync( model.Url, model.Url );
		//	}
		//	catch ( Exception ex )
		//	{
		//	}

		//	return GenerateSitemapUpdate(crawler.WebsiteUrl, "Crawling started...", "" );
		//}


		public ContentResult GenerateSitemap( [Bind( Prefix = "WebsiteRoot" )] string WebsiteRoot, [Bind( Prefix = "CrawlUrl" )] string CrawlUrl, [Bind( Prefix = "SearchEngine" )] string SearchEngine )
		{
			Crawler crawler = new Crawler();
			try
			{
				Session[ "sitemapUrl" ] = CrawlUrl;
				crawler.IncludeOptions.IncludeImage = true;
				//crawler.IncludeLastModifiedTag = true;
				//crawler.IncludeChangeFrequencyTag = true;
				//crawler.IncludePriorityTag = true;
				crawler.ExcludeUrlsContaining = new List<string>();
				crawler.ExcludeUrlsContaining.Add( "/account" );
				crawler.ExcludeUrlsContaining.Add( "/authentication" );

				crawler.ExcludeImageUrlsContaining = new List<string>();
				crawler.ExcludeImageUrlsContaining.Add( "/images/" );
				crawler.ExcludeImageUrlsContaining.Add( "/account-images/" );

				crawler.CrawlingCompleted += new CrawlingCompletedEventHandler( CrawlingCompleted_CrawlingCompletedEventHandler );
				crawler.CrawlingStatusChanged += new CrawlingStatusChangedEventHandler( CrawlingStatusChanged_CrawlingStatusChangedEventHandler );

				Sitemap sitemap = crawler.Crawl( CrawlUrl, WebsiteRoot );
				string sXml = sitemap.GetXml( SupportedSearchEngine.Google );
				sXml = sXml.Replace( "<", "&lt;" ).Replace( ">", "&gt;" )
					.Replace( "\"", "&quot;" ).Replace( "\r\n", "<br/>" )
					.Replace( "\t", "&nbsp;" );
				//HtmlString hString = new HtmlString( sXml );
				//sXml = hString.ToHtmlString();
				return this.UpdateSitemap( crawler.WebsiteUrl, sXml );
			}
			catch ( Exception ex )
			{
				return UpdateSitemap( crawler.WebsiteUrl, ex.Message );
			}
		}

		private void CrawlingCompleted_CrawlingCompletedEventHandler( object sender, CrawlingCompletedEventArgs e )
		{
			try
			{
				//string sXml = e.Sitemap.GetXml( ( SupportedSearchEngine ) Enum.Parse( typeof( SupportedSearchEngine ), sSearchEngine ) );
				string sXml = e.Sitemap.GetXml( SupportedSearchEngine.Google );
				Crawler crawler = sender as Crawler;
				UpdateSitemap( crawler == null ? "" : crawler.WebsiteUrl, sXml );
			}
			catch ( Exception ex )
			{
			}
		}

		private void CrawlingStatusChanged_CrawlingStatusChangedEventHandler( object sender, CrawlingStatusChangedEventArgs e )
		{
			//string sUrl = Session[ "sitemapUrl" ] == null ? "" : Session[ "sitemapUrl" ].ToString();
			Crawler crawler = sender as Crawler;
			UpdateStatus( crawler == null ? "" : crawler.WebsiteUrl, e.StatusMessage );
		}

		public ContentResult UpdateStatus( string url, string status )
		{
			Models.SitemapModel model = new Models.SitemapModel();
			model.Url = url;
			model.Sitemap = "";
			model.Status = status;
			ViewBag.Message = "Welcome to Sitemap Generator!";
			return Content( status, "text" );
		}

		public ContentResult UpdateSitemap( string url, string xml )
		{
			Models.SitemapModel model = new Models.SitemapModel();
			model.Url = url;
			model.Sitemap = xml;
			model.Status = "Completed";
			ViewBag.Message = "Welcome to Sitemap Generator!";
			return Content( xml, "text" );
		}

	}
}
